package com.goparty.data.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.goparty.data.exception.BaseException;
import com.goparty.data.model.Event;
import com.goparty.data.model.EventMessage; 
import com.goparty.data.model.MessageRead;
import com.goparty.data.repository.IMessageRepository;
import com.goparty.data.repository.IUserRepository;
@Repository("messageDao")
@Transactional
public class MessageDao {
	
	private static Log log = LogFactory.getLog(MessageDao.class);
	
	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	private IMessageRepository messageRepository;
	
	 
	@Autowired
	private IUserRepository userRepository;
	 
	
	public EventMessage read(String id) {
		EventMessage message = messageRepository.findOne(id);
		return message;
	}

	public EventMessage create(EventMessage msg) { 		 
		msg = messageRepository.save(msg); 
		return msg;
	}
 
	public boolean update(String id, String message) {	 
		EventMessage updateMsg = messageRepository.findOne(id);
		if(updateMsg!=null){
			Query query = em.createNativeQuery("update gp_event_message set message=:message where id=:id");
			query.setParameter("id", id);
			query.setParameter("message", message);
			query.executeUpdate();
		}else{
			throw new BaseException("message not exist.");			
		}
		return true;		
	}

	public boolean delete(String id) {
		boolean ret = false;		
		try{
			messageRepository.delete(id);
			ret = true;
		}catch(Exception ex){
			log.error("del user error",ex);
			throw ex;
		}		
		return ret;
	}
	 
	public List<EventMessage> getMessages(String eventId,Date after, Date before,int offset,int limit){	
		Query query = em.createNativeQuery("select * from gp_event_message where publishTime>=:after and publishTime<=:before"
				+ " and eventId=:eventId order by publishTime asc limit :offset, :limit",EventMessage.class);
		query.setParameter("eventId", eventId);
		query.setParameter("offset", offset);
		query.setParameter("limit", limit);
		query.setParameter("after", after);
		query.setParameter("before", before);
		List<EventMessage> msgList = query.getResultList();	 
		return msgList;
	}

	

	public List<EventMessage> getUnreadMessages(String userId,String eventId, int offset, int limit) {
		Query query = em.createNativeQuery("select m.* from gp_event_message m join gp_message_read_record r on m.id=r.messageId"
				+ " where r.readed=0 and r.userId=:userId and r.eventId=:eventId "
				+ " order by publishTime asc limit :offset, :limit",EventMessage.class);
		query.setParameter("userId", userId);
		query.setParameter("eventId", eventId);
		query.setParameter("offset", offset);
		query.setParameter("limit", limit);
		List<EventMessage> msgList = query.getResultList(); 
		return msgList;
	} 
	


	public void addMessageReadRecord(MessageRead mr) {
		em.persist(mr);
	}
	
	public void updateReadStatus(String userId,String messageId) { 
		Query  q = em.createNativeQuery("update gp_message_read_record set readed=1 where userId=:userId and messageId=:messageId");
		q.setParameter("userId", userId);
		q.setParameter("messageId", messageId);
		q.executeUpdate();
	 
	}
	
}
